Plotly

We’re making interactive plots

library(tidyverse)
library(plotly)
library(dplyr)

library(p8105.datasets)

Focus on NYC Airbnb data.

data("rest_inspec")

rest_inspec_bronx = rest_inspec |> 
  select(boro, building, critical_flag, cuisine_description, dba, score, zipcode) |> 
  drop_na(score) |> 
  filter( boro == "BRONX", score >=30) 

rest_inspec_allboros = rest_inspec |> 
  select(boro, building, critical_flag, cuisine_description, dba, score, zipcode, inspection_date) |> 
  drop_na(score) |> 
  filter(score >=30) |> sample_n(1000, replace = FALSE)


rest_inspec |> count(cuisine_description)
## # A tibble: 85 × 2
##    cuisine_description     n
##    <chr>               <int>
##  1 Afghan                200
##  2 African              1444
##  3 American            90029
##  4 Armenian              500
##  5 Asian                6238
##  6 Australian            242
##  7 Bagels/Pretzels      2864
##  8 Bakery              11884
##  9 Bangladeshi           905
## 10 Barbecue              856
## # ℹ 75 more rows

Let’s make a scatterplot!

rest_inspec_allboros %>%
  plot_ly(
    x = ~inspection_date, y = ~score, color = ~boro, 
    type = "scatter", mode = "markers", marker = list(size = 10, opacity = 0.5)) 

Let’s make a box plot!

 rest_inspec %>% filter(boro != "Missing", score >=0) |> 
  mutate(boro = fct_reorder(boro, score)) |> 
  plot_ly(
    x = ~boro, y = ~score,
    type = "box", colors = "viridis") 

Let’s make a bar plot!

 rest_inspec_bronx %>%
  count(cuisine_description) %>%
  mutate(cuisine_description = fct_reorder(cuisine_description, n)) %>%
  plot_ly(
    x = ~cuisine_description, y = ~n, color = ~cuisine_description,
    type = "bar", colors = "viridis")